

cc.Class({
    extends: cc.Component,

    properties: {
        //显示排行榜的图片.
        rankDisplay: cc.Sprite,
        loadingNode: cc.Node,
        helpPanel: cc.Node,
        //声音开启的图标.
        bgmBtnSprite: cc.Sprite,
        bgmOnSprite: cc.SpriteFrame,
        bgmOffSprite: cc.SpriteFrame
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        console.log('Global.debug:'+Global.debug);
        //判断是否首次进入.
        var firstIn = cc.sys.localStorage.getItem('first');
        if(firstIn == 'false'){
            //本地首次进入.
            Global.isFirstIn = false;
        }
        var bgmState = cc.sys.localStorage.getItem('bgm');
        
        if(bgmState != null){
            if(bgmState == 'true'){
                this.bgmBtnSprite.spriteFrame = this.bgmOnSprite;
                Global.bgmOn = true;
            }else if(bgmState == 'false'){
                this.bgmBtnSprite.spriteFrame = this.bgmOffSprite;
                Global.bgmOn = false;
            }
        }
    },

    start () {
        //是否打开了排行榜.
        this.isShowRankPanel = false;
        this.rankTex = new cc.Texture2D();
        //显示最高分.
        if(!Global.debug){
            wx.postMessage({
                message: 'ShowHighScore'
            });
        }
    },

    update (dt) {
        //发布微信开启！
        if(Global.debug) return;
        this.updaetSubDomainCanvas();
    },

    //开始游戏.
    startGame: function(){
        if(!Global.debug){
            wx.postMessage({
                message: 'GameScene'
            });
        }
        this.loadingNode.active = true;
        cc.director.loadScene('Game');
    },

    //显示好友排行榜按钮.
    onRankBtnDown(){
        wx.postMessage({
            message: 'ShowRank'
        });
    },

    //更新子域的排行榜显示.
    updaetSubDomainCanvas () {
        if (!this.rankTex) {
            return;
        }
        var openDataContext = wx.getOpenDataContext();
        var sharedCanvas = openDataContext.canvas;
        this.rankTex.initWithElement(sharedCanvas);
        this.rankTex.handleLoadedTexture();
        this.rankDisplay.node.setContentSize(cc.director.getVisibleSize());
        this.rankDisplay.spriteFrame = new cc.SpriteFrame(this.rankTex);
    },

    onHelpBtnDown: function(){
        this.helpPanel.active = true;
    },

    onHelpYesDown: function(){
        Global.isFirstIn = true;
        this.startGame();
    },

    onHelpNoDown: function(){
        this.helpPanel.active = false;
    },

    changeBgmState(){
        Global.bgmOn = !Global.bgmOn;
        //换图片.
        this.bgmBtnSprite.spriteFrame = Global.bgmOn ? this.bgmOnSprite : this.bgmOffSprite;
        cc.sys.localStorage.setItem('bgm', Global.bgmOn.toString());
    }
});
